/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package helpdiabetesj2me;

import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;

/**
 *
 * @author Johan
 */
public class ReportException implements CommandListener {  
     
     static String str_foutm[] = {"Foutmelding"};
     static String str_ok [] = {"Ok"};
    boolean exitapp;
    Command done ;

    Displayable curdisp;
    //method to report an exception to the user
    //usertext : text defined by myself in the code to give more explanation
    //e : the exception
    //fatal : if true, application will be ended by this method, if false means application can continue
    // curdisp : the display to switch to after user has dismissed the alert
    // Alert will be shown with usertext, e.toString
    // After dismissing, and if not fatal, the previous displayable will be reshown
    public void  Report(String usertext,Exception e, boolean fatal)  {
        exitapp=fatal;
        curdisp =  HelpDiabetes.midlet.getDisplay().getCurrent();
        
        Alert myalert = new Alert(str_foutm[HelpDiabetes.languageIndex],
                    usertext + 
                    "\n\ne.toString() = \n" + 
                    e.toString() +
                    "\n\ne.getMessage() =\n" + 
                    e.getMessage(),null,AlertType.ERROR);
        done = new Command(str_ok[HelpDiabetes.languageIndex],Command.EXIT,0);
        myalert.addCommand(done);
        myalert.setCommandListener(this);
        myalert.setTimeout(Alert.FOREVER);
        
        
        // switch display to myAlert
	HelpDiabetes.midlet.getDisplay().setCurrent(myalert);
                
        myalert = null;
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == done) {
            if (exitapp) {
                FoodFile.deletefoodfileRMS();
                try {HelpDiabetes.selitemsrs.closeRecordStore();} catch (Exception ex) {;}
                try {RecordStore.deleteRecordStore("selecteditems");} catch (Exception ex) {;}        
                HelpDiabetes.selitemsrs = null;
                HelpDiabetes.midlet.exitMIDlet();
            } else {
                HelpDiabetes.midlet.getDisplay().setCurrent(curdisp);
            }
        }
    }
   
}
